#pragma once
#include <iostream>
#include <string>

using namespace std;

#include <cstdio>
#include <stdarg.h>
#include <ctime>
#include <cerrno>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define Info 0
#define Debug 1
#define Warning 2
#define Error 3
#define Fatal 4

#define SIZE 1024

#define Screen 1
#define Onefile 2
#define Classfile 3

#define DEFAULT_FILE "log.txt"

class Log
{
public:
  Log()
  {
    _printmethod = Screen;
    _path = "./log/";
  }
  void Enable(int method)
  {
    _printmethod = method;
  }
  const char *leveltoString(int level)
  {
    switch (level)
    {
    case Info:
      return "Info";
    case Debug:
      return "Debug";
    case Warning:
      return "Warning";
    case Error:
      return "Error";
    case Fatal:
      return "Fatal";
    default:
      return nullptr;
    }
  }

  void logMessage(int level, const char *format, ...)
  {
    time_t t = time(nullptr);
    struct tm *ctime = localtime(&t);
    // 左半的日志信息
    char leftbuffer[SIZE];
    snprintf(leftbuffer, sizeof(leftbuffer), "[%s] [%d-%d-%d %d:%d:%d]", leveltoString(level),
             ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday,
             ctime->tm_hour, ctime->tm_min, ctime->tm_sec);
    // 右半的日志信息
    char rightbuffer[SIZE];
    va_list s;
    va_start(s, format);
    vsnprintf(rightbuffer, sizeof(rightbuffer), format, s);
    va_end(s);

    // 全部的信息 默认+自定义
    char logtxt[SIZE * 2];
    snprintf(logtxt, sizeof(logtxt), "%s %s", leftbuffer, rightbuffer);

    // 暂时打印
    // printf("%s\n", logtxt);
    printlog(level, logtxt);
  }

  // ----------------------------------------------------
  // 重载operator()，便于日志的打印
  void operator()(int level, const char *format, ...)
  {
    time_t t = time(nullptr);
    struct tm *ctime = localtime(&t);
    // 左半的日志信息
    char leftbuffer[SIZE];
    snprintf(leftbuffer, sizeof(leftbuffer), "[%s] [%d-%d-%d %d:%d:%d]", leveltoString(level),
             ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday,
             ctime->tm_hour, ctime->tm_min, ctime->tm_sec);
    // 右半的日志信息
    char rightbuffer[SIZE];
    va_list s;
    va_start(s, format);
    vsnprintf(rightbuffer, sizeof(rightbuffer), format, s);
    va_end(s);

    // 全部的信息 默认+自定义
    char logtxt[SIZE * 2];
    snprintf(logtxt, sizeof(logtxt), "%s %s", leftbuffer, rightbuffer);

    // 暂时打印
    // printf("%s\n", logtxt);
    printlog(level, logtxt);
  }

  //-----------------------------------------------------

  void printlog(int level, const string &logtxt)
  {
    switch (_printmethod)
    {
    case Screen:
      printf("%s\n", logtxt.c_str());
      break;
    case Onefile:
      printOnefile(DEFAULT_FILE, logtxt);
      break;
    case Classfile:
      printClassfile(level, logtxt);
      break;
    }
  }

  void printOnefile(const string &logname, const string &logtxt)
  {
    string pathname = _path + logname;
    int fd = open(pathname.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0666);
    if (fd < 0)
      return;
    write(fd, logtxt.c_str(), logtxt.size());
    close(fd);
  }
  void printClassfile(int level, const string &logtxt)
  {
    string filename = DEFAULT_FILE;
    filename += ".";
    filename += leveltoString(level);
    printOnefile(filename, logtxt);
  }

private:
  int _printmethod;
  string _path;
};

// int sum(int n, ...)
// {
//   va_list s;      // char* 类型
//   va_start(s, n); // 让s指向n的地址的下一个地址，也就是第一个可变参数的地址

//   int sum = 0;
//   while (n)
//   {
//     sum += va_arg(s, int);
//     n--;
//   }
//   va_end(s); // 将 s 置空
//   return sum;
// }

// int sum(int n, const char *format, ...)
// {
//   va_list s;           // char* 类型
//   va_start(s, format); // 让s指向第一个可变参数的地址

//   int sum = 0;
//   while (n)
//   {
//     sum += va_arg(s, int);
//     n--;
//   }
//   va_end(s); // 将 s 置空
//   return sum;
// }
